﻿using Kylin.Wiki.IService;
using Kylin.Wiki.Model.Api;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Model.Entity;
using Kylin.Wiki.Model.View;
using Kylin.Wiki.Web.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Kylin.Wiki.Web.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeptController : ControllerBase
    {
        private readonly IDeptService _deptService;

        public DeptController(IDeptService deptService)
        {
            this._deptService = deptService;
        }

        [HttpGet("GetList")]
        public ApiResult<List<Dept>> GetList()
        {
            ApiResult<List<Dept>> result = new ApiResult<List<Dept>>();
            result.Data = _deptService.Find(x => 1 == 1);
            return result;
        }

        [HttpGet("GetListByPage")]
        public ApiPageResult<List<DeptVM>> GetListByPage(DeptDTO deptDTO)
        {
            return _deptService.GetListByPage(deptDTO);
        }

        [HttpPost]
        public ApiResult<bool> Create([FromBody] Dept dept)
        {
            dept.CreateBy = UserContext.Current.UserName;
            dept.CreateOn = DateTime.Now;
            dept.UpdateBy = UserContext.Current.UserName;
            dept.UpdateOn = DateTime.Now;
            dept.IsDelete = "0";
            dept.Status = false;
            return _deptService.Create(dept);
        }

        [HttpPut]
        public ApiResult<bool> Update([FromBody] Dept dept)
        {
            dept.UpdateBy = UserContext.Current.UserName;
            dept.UpdateOn = DateTime.Now;
            return _deptService.Update(dept);
        }

        [HttpDelete("{id}")]
        public ApiResult<bool> Delete(int id)
        {
            var role = _deptService.FindFirst(x => x.DeptID == id);
            if (role == null)
                return new ApiResult<bool>(false, Core.Enum.HttpStateCode.ParameterError, "该角色不存在");
            return _deptService.Delete(role);
        }

            
    }
}
